home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / grafikkarten / deinterlace / deinterlace.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  10KB  |  315 lines

  1. /* Thomas Reinhardt */
  2. #include <time.h>
  3. #include <string.h>
  4. #include <exec/interrupts.h>
  5. #include <exec/libraries.h>
  6. #include <exec/lists.h>
  7. #include <exec/memory.h>
  8. #include <exec/nodes.h>
  9. #include <exec/ports.h>
  10. #include <exec/types.h>
  11. #include <intuition/intuition.h>
  12. #include <libraries/dos.h>
  13. #include <libraries/dosextens.h>
  14. #include <proto/dos.h>
  15. #include <proto/exec.h>
  16. #include <proto/graphics.h>
  17. #include <proto/intuition.h>
  18. #include <proto/gadtools.h>
  19. #include <proto/diskfont.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #include "DeInterlace.h"
  24.  
  25. /*
  26. #define abs(x) ((x)<0?-(x):(x))
  27. */
  28.  
  29. LONG FileNamePointer,SaveNamePointer;
  30. struct IntuitionBase *IntuitionBase;
  31. struct GfxBase *GfxBase;
  32. struct Library *DosBase;
  33. struct NewScreen MyNewScreen;
  34. struct Screen *MyScreen;
  35. struct NewWindow VorWin;
  36. struct Window *MyWindow;
  37. struct RastPort *MyRastPort;
  38. struct BitMapHeader BMHD;
  39. struct ColorRegister Colors[256];
  40. struct CommodoreAmiga CAMG;
  41. int Mask[9]={1,2,4,8,16,32,64,128,256};
  42. LONG Len,x,y,b;
  43. ULONG ChunkLen;
  44. BOOL BMHDFlag,CMAPFlag,CAMGFlag,BODYFlag,FoundChunk,ShowFlag=FALSE;
  45. UBYTE Buffer[1000];
  46. UBYTE ByteCount;
  47. UBYTE BytesPerRow;
  48. char *WohinDamit;
  49. BOOL success,AllDone;
  50. int i;
  51. char inread[5];
  52.  
  53. int main(int argc,char **argv){
  54.     if(argc==3){
  55.         DosBase=(struct Library *)OpenLibrary("dos.library",0L);
  56.         GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  57.         IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  58.         if((DosBase==0)||(GfxBase==0)||(IntuitionBase==0)) DisplayErrorMessage("Error opening libraries");
  59.       BMHDFlag=CMAPFlag=CAMGFlag=BODYFlag=FALSE;
  60.         FileNamePointer=Open(argv[1],MODE_OLDFILE);
  61.         if(!FileNamePointer){
  62.             DisplayErrorMessage("Fehler : Datei nicht gefunden!");
  63.             }
  64.         else{
  65.             AllDone=FALSE;
  66.             success=Einlesen(&Buffer[0],12,TRUE);
  67.             if(strncmp(&Buffer[0],"FORM",4)==0){
  68.                 printf("Kennung >FORM< found ...\n");
  69.                 if(strncmp(&Buffer[8],"ILBM",4)==0){
  70.                     printf("Kennung >ILBM< found ...\n");
  71.                     Loop:
  72.                     FoundChunk=FALSE;
  73.                     Einlesen(&Buffer[0],8,FALSE);
  74.                     if(Len<=0){
  75.                         if((BMHDFlag==TRUE)&&(BODYFlag==TRUE)&&(CMAPFlag==TRUE)){
  76.                             if(CAMGFlag==FALSE) printf("Kein CommodoreAmiga!\n");
  77.                             DeInterlace();
  78.                             SaveFile(argv[2]);
  79.                             DisplayErrorMessage("Operation ended...");
  80.                             }
  81.                         else{
  82.                             if(BMHDFlag!=TRUE) DisplayErrorMessage("Kein BMHD");
  83.                             if(BODYFlag!=TRUE) DisplayErrorMessage("Kein BODY");
  84.                             if(CMAPFlag!=TRUE) DisplayErrorMessage("Kein CMAP");
  85.                             }
  86.                         }
  87.                     ChunkLen=Buffer[4]*16777216+Buffer[5]*65536+Buffer[6]*256+Buffer[7];
  88.                     if(strncmp(Buffer,"BMHD",4)==0){
  89.                         printf("Kennung >BMHD< found ...\n");
  90.                         if(BMHDFlag==TRUE) DisplayErrorMessage("Found second BMHD !");
  91.                         Einlesen(&BMHD,ChunkLen,TRUE);
  92.                         MyNewScreen.LeftEdge=0;
  93.                         MyNewScreen.TopEdge=0;
  94.                         MyNewScreen.Width=BMHD.Width;
  95.                         MyNewScreen.Height=BMHD.Height;
  96.                         MyNewScreen.Depth=BMHD.nPlanes;
  97.                         MyNewScreen.DetailPen=1;
  98.                         MyNewScreen.BlockPen=0;
  99.                         if(CAMGFlag==TRUE) MyNewScreen.ViewModes=CAMG.ViewModes;
  100.                         else{
  101.                             MyNewScreen.ViewModes=0;
  102.                             if(MyNewScreen.Width>320) MyNewScreen.ViewModes|=HIRES;
  103.                             if(MyNewScreen.Height>256) MyNewScreen.ViewModes|=LACE;
  104.                             }
  105.                         MyNewScreen.Type=CUSTOMSCREEN;
  106.                         MyNewScreen.Font=NULL;
  107.                         MyNewScreen.DefaultTitle=(char *)argv[1];
  108.                         MyNewScreen.Gadgets=NULL;
  109.                         MyNewScreen.CustomBitMap=NULL;
  110.                         MyScreen=OpenScreen(&MyNewScreen);
  111.                         if(MyScreen==0) DisplayErrorMessage("No Screen ???");
  112.                         BytesPerRow=BMHD.w/8;
  113.                         if(((float)BytesPerRow)!=(((float)BMHD.w)/8)){
  114.                             BytesPerRow++;
  115.                             }
  116.                         ShowTitle(MyScreen,FALSE);
  117.                         VorWin.LeftEdge=0;
  118.                         VorWin.TopEdge=0;
  119.                         VorWin.Width=BMHD.w;
  120.                         VorWin.Height=BMHD.h;
  121.                         VorWin.DetailPen=0;
  122.                         VorWin.BlockPen=0;
  123.                         VorWin.Title=NULL;
  124.                         VorWin.Flags=ACTIVATE|BORDERLESS|BACKDROP|SMART_REFRESH;
  125.                         VorWin.IDCMPFlags=MOUSEMOVE|MOUSEBUTTONS;
  126.                         VorWin.FirstGadget=NULL;
  127.                         VorWin.CheckMark=NULL;
  128.                         VorWin.Type=CUSTOMSCREEN;
  129.                         VorWin.Screen=MyScreen;
  130.                         VorWin.BitMap=NULL;
  131.                         VorWin.MinWidth=BMHD.w;
  132.                         VorWin.MinHeight=BMHD.h;
  133.                         VorWin.MaxWidth=BMHD.w;
  134.                         VorWin.MaxHeight=BMHD.h;
  135.                         if(NULL!=(MyWindow=OpenWindow(&VorWin))){
  136.                             ShowTitle(MyScreen,FALSE);
  137.                             MyRastPort=MyWindow->RPort;
  138.                             SetDrMd(MyRastPort,JAM2);
  139.                             }
  140.                         BMHDFlag=TRUE;
  141.                         FoundChunk=TRUE;
  142.                         }
  143.                     if(strncmp(Buffer,"CMAP",4)==0){
  144.                         printf("Kennung >CMAP< found ...\n");
  145.                         if(CMAPFlag==TRUE) DisplayErrorMessage("found second CMAP!");
  146.                         if(BMHDFlag==FALSE) DisplayErrorMessage("need at first BMHD");
  147.                         Einlesen(Colors,ChunkLen,TRUE);
  148.                         for(i=0;i<Mask[BMHD.nPlanes];i++){
  149.                             SetRGB4(&MyScreen->ViewPort,i,Colors[i].rot>>4,Colors[i].gruen>>4,Colors[i].blau>>4);
  150.                             }
  151.                         CMAPFlag=TRUE;
  152.                         FoundChunk=TRUE;
  153.                         }
  154.                     if(strncmp(Buffer,"BODY",4)==0){
  155.                         printf("Kennung >BODY< found ...\n");
  156.                         if(BODYFlag==TRUE) DisplayErrorMessage("found second BODY!");
  157.                         if(BMHDFlag==FALSE) DisplayErrorMessage("need at first BMHD");
  158.                         for(y=0;y<BMHD.h;y++){
  159.                             for(b=0;b<BMHD.nPlanes;b++){
  160.                                 ByteCount=0;
  161.                                 WohinDamit=(char *)MyScreen->RastPort.BitMap->Planes[b]+y*(BMHD.Width/8);
  162.                                 if(BMHD.compression==0)
  163.                                     Einlesen(WohinDamit,BytesPerRow,TRUE);
  164.                                 if(BMHD.compression==1){
  165.                                     while(ByteCount<BytesPerRow){
  166.                                         Einlesen(&Buffer[0],1,TRUE);
  167.                                         if(Buffer[0]<128){
  168.                                             Einlesen(WohinDamit+ByteCount,Buffer[0]+1,TRUE);
  169.                                             ByteCount+=Buffer[0]+1;
  170.                                             }
  171.                                         if(Buffer[0]>128){
  172.                                             Einlesen(&Buffer[1],1,TRUE);    
  173.                                             for(i=ByteCount;i<(ByteCount+257-Buffer[0]);i++) *(WohinDamit+i)=Buffer[1];
  174.                                             ByteCount+=257-Buffer[0];
  175.                                             }
  176.                                         }
  177.                                     }
  178.                                 }
  179.                             }
  180.                         BODYFlag=TRUE;
  181.                         FoundChunk=TRUE;
  182.                         AllDone=TRUE;
  183.                         }
  184.                     if(strncmp(Buffer,"CAMG",4)==0){
  185.                         printf("Kennung >CAMG< found ...\n");
  186.                         if(CAMGFlag==TRUE) DisplayErrorMessage("found second CAMG");
  187.                         if(BMHDFlag==FALSE) DisplayErrorMessage("need first BMHD");
  188.                         Einlesen(&CAMG,ChunkLen,TRUE);
  189.                         MyScreen->ViewPort.Modes=CAMG.ViewModes;
  190.                         RemakeDisplay();
  191.                         CAMGFlag=TRUE;
  192.                         FoundChunk=TRUE;
  193.                         }
  194.                     if((FoundChunk==FALSE)&&(AllDone==FALSE)){
  195.                         Einlesen(&Buffer,ChunkLen,FALSE);
  196.                         if((ChunkLen&1)==1) Einlesen(Buffer,1,FALSE);
  197.                         }
  198.                     goto Loop;
  199.                     }
  200.                 else{
  201.                     DisplayErrorMessage("Falsches InputFile!");
  202.                     }
  203.                 }
  204.             else{
  205.                 DisplayErrorMessage("Falsches InputFile!");
  206.                 }
  207.             }
  208.         DisplayErrorMessage("all done ... aborting!");
  209.         return 0;
  210.         }
  211.     else{
  212.         DisplayErrorMessage("USAGE : DeInterlace <infile> <outfile>");
  213.         }
  214.     }
  215.  
  216. BOOL Einlesen(char *Buffer,WORD Anz,BOOL Flag){
  217.     Len=Read(FileNamePointer,Buffer,Anz);
  218.     if((Flag==TRUE)&&(Len<0)) DisplayErrorMessage("File is corrupt!");
  219.     return TRUE;
  220.     }
  221.  
  222. BOOL SaveFile(char *OpenFileName){
  223.     ULONG zwsp[1];
  224.   int yi,b;
  225.  
  226.     SaveNamePointer=Open(OpenFileName,MODE_NEWFILE);
  227.     if(!SaveNamePointer){
  228.         DisplayErrorMessage("Error : Save-Gfx(IFF)-Datei!");
  229.         }
  230.     else{
  231.         Write(SaveNamePointer,"FORM",4);
  232.         zwsp[0]=4;    /* ILBM */
  233.         zwsp[0]+=sizeof(BMHD)+8; /* BMHD */
  234.         zwsp[0]+=Mask[BMHD.nPlanes]*3*3; /* CMAP */
  235.         zwsp[0]+=8; /* CMAP-Header */
  236.         zwsp[0]+=12; /* CAMG */
  237.         zwsp[0]+=BMHD.Width*BMHD.Height*BMHD.nPlanes/8+8; /* BODY */
  238.         Write(SaveNamePointer,&zwsp[0],4);
  239.         Write(SaveNamePointer,"ILBM",4);
  240.         Write(SaveNamePointer,"CMAP",4);
  241.         zwsp[0]=Mask[BMHD.nPlanes]*3*3;
  242.         Write(SaveNamePointer,&zwsp[0],4);
  243.         Write(SaveNamePointer,&Colors[0],Mask[BMHD.nPlanes]*3*3);
  244.         Write(SaveNamePointer,"CAMG",4);
  245.         zwsp[0]=4;
  246.         Write(SaveNamePointer,&zwsp[0],4);
  247.         zwsp[0]=4;
  248.         Write(SaveNamePointer,&CAMG,4);
  249.         Write(SaveNamePointer,"BMHD",4);
  250.         BMHD.compression=0;
  251.         BMHD.masking=0;
  252.         zwsp[0]=sizeof(BMHD);
  253.         Write(SaveNamePointer,&zwsp[0],4);
  254.         Write(SaveNamePointer,&BMHD,sizeof(BMHD));
  255.         Write(SaveNamePointer,"BODY",4);
  256.         zwsp[0]=BMHD.Width*BMHD.Height*BMHD.nPlanes/8;
  257.         Write(SaveNamePointer,&zwsp[0],4);
  258.         for(yi=0;yi<BMHD.Height;yi++){
  259.             for(b=0;b<BMHD.nPlanes;b++){
  260.                 Write(SaveNamePointer,MyScreen->RastPort.BitMap->Planes[b]+yi*(BMHD.Width/8),BMHD.Width/8);
  261.                 }
  262.             }
  263.         }
  264.     return TRUE;
  265.     }
  266.  
  267. void DisplayErrorMessage(char *Meldung){
  268.     printf("%s\n",Meldung);
  269.     if(MyWindow) CloseWindow(MyWindow);
  270.     if(FileNamePointer) Close(FileNamePointer);
  271.     if(SaveNamePointer) Close(SaveNamePointer);
  272.     if(MyScreen) CloseScreen(MyScreen);
  273.     if(DosBase) CloseLibrary(DosBase);
  274.     if(GfxBase) CloseLibrary((struct Library *)GfxBase);
  275.     if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  276.     exit(TRUE);
  277.     }
  278.  
  279. void DeInterlace(){
  280.     int xx,yy;
  281.     UBYTE Zwsp;
  282.     LONG Red1,Blue1,Green1,Red2,Blue2,Green2,RedNeu,BlueNeu,GreenNeu;
  283.     LONG dummy,dummyNr,dummyMax;
  284.     for(yy=1;yy<BMHD.h;yy=yy+2){
  285.         for(xx=0;xx<BMHD.w;xx++){
  286.             Zwsp=ReadPixel(MyRastPort,xx,yy-1);
  287.             Red1=Colors[Zwsp].rot;
  288.             Blue1=Colors[Zwsp].blau;
  289.             Green1=Colors[Zwsp].gruen;
  290.             Zwsp=ReadPixel(MyRastPort,xx,yy+1);
  291.             Red2=Colors[Zwsp].rot;
  292.             Blue2=Colors[Zwsp].blau;
  293.             Green2=Colors[Zwsp].gruen;
  294.             RedNeu=(Red1+Red2)/2;
  295.             GreenNeu=(Green1+Green2)/2;
  296.             BlueNeu=(Blue1+Blue2)/2;
  297.             dummyMax=256*3;
  298.             dummy=0;
  299.             dummyNr=-1;
  300.             for(i=0;i<Mask[BMHD.nPlanes];i++){
  301.                 dummy=abs(RedNeu-Colors[i].rot);
  302.                 dummy=dummy+abs(BlueNeu-Colors[i].blau);
  303.                 dummy=dummy+abs(GreenNeu-Colors[i].gruen);
  304.                 if (dummy<dummyMax){
  305.                     dummyMax=dummy;
  306.                     dummyNr=i;
  307.                     }
  308.                 dummy=0;
  309.                 }
  310.             SetAPen(MyRastPort,dummyNr);
  311.             WritePixel(MyRastPort,xx,yy);
  312.             }        
  313.         }
  314.     }
  315.